package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.utils.UploadUtil;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ServletContext ctx;


    @RequestMapping("list")
    public String list(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        qo.setState(Travel.STATE_CHECKING);
        qo.setOrderBy("t.createTime desc");
        qo.setIsPublic(true);
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/travel/list";
    }


    @RequestMapping("releaseList")
    public String releaseList(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        qo.setState(Travel.STATE_RELEASE);
        qo.setOrderBy("t.createTime desc");
        qo.setIsPublic(true);
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/release/list";
    }


    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Travel travel){
        JSONResult result = new JSONResult();
        try {
            //如果状态为发布状态,设置发布时间
            if(travel.getState()==Travel.STATE_RELEASE){
                travel.setReleaseTime(new Date());
            }
            //更改状态
            travelService.updateState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }



    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        TravelContent travelContent = travelService.getTravelContentById(id);
        return travelContent;
    }

    @RequestMapping("/images")
    @ResponseBody
    public Object updateImg(MultipartFile multipartFile){
        //为了兼容后面的操作,提前设计一个status的属性,如果传递的status=1,代表成功
        //注意:如果用domain无法封装的,可以用map
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            System.out.println(ctx.getRealPath("/upload"));
            String imagePath = UploadUtil.upload(multipartFile, UploadUtil.IMG_BASE_PATH+"/upload");
            map.put("url",imagePath);
            map.put("status",1);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("url","");
            map.put("status",0);
        }
        return map;
    }







}
